package com.zglc.ms.shop.service;

import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.data.redis.connection.jedis.JedisConnection;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.stereotype.Component;

import redis.clients.jedis.Jedis;

@Component
public class CacheManagerUtil {

    @Autowired
    private CacheManager cacheManager;
    
    @Autowired
    private JedisConnectionFactory jedisConnectionFactory;
    
    private final String DEFAULT_CACHE = "DEFAULT_CACHE";
    
    public static final String CACHE_BUYING = "CACHE_SHOP_BUYING";
    //首页缓存有效时间(20秒)
    public static final int SHOW_VALID_TIME = 1*20;
    //支付有效时间(5分钟)
    public static final int PAY_VALID_TIME = 15*60;
    
    public static final int CACHE_FIVE_MINUTE = 5*60*60;
    
    public long getLong(String key, long amount){
    	JedisConnection jedisConnection = jedisConnectionFactory.getConnection();
    	Jedis jedis = jedisConnection.getNativeConnection();
    	try {
        	return jedis.incrBy(key, amount);
		} finally {
			jedisConnection.close();
		}
    	
    }
    
    public boolean isExist(String key){
    	JedisConnection jedisConnection = jedisConnectionFactory.getConnection();
    	Jedis jedis = jedisConnection.getNativeConnection();
    	try {
    		return jedis.exists(key);
		} finally {
			jedisConnection.close();
		}
    	
    }
    
    public void jSet(String key, String value, int timeout){
    	JedisConnection jedisConnection = jedisConnectionFactory.getConnection();
    	Jedis jedis = jedisConnection.getNativeConnection();
    	try {
    		jedis.setex(key, timeout, value);
		} finally {
			jedisConnection.close();
		}
    }
    
    public String jGet(String key){
    	JedisConnection jedisConnection = jedisConnectionFactory.getConnection();
    	Jedis jedis = jedisConnection.getNativeConnection();
    	try {
    		return jedis.get(key);
		} finally {
			jedisConnection.close();
		}
    }
    
    public void jDel(String key){
    	JedisConnection jedisConnection = jedisConnectionFactory.getConnection();
    	Jedis jedis = jedisConnection.getNativeConnection();
    	try {
    		jedis.del(key);
		} finally {
			jedisConnection.close();
		}
    	
    }
    
    public long getAllBuyingCnt(String keys){
    	JedisConnection jedisConnection = jedisConnectionFactory.getConnection();
    	Jedis jedis = jedisConnection.getNativeConnection();
    	try {
    		Set<String> keySet = jedis.keys(keys + "*");
        	long cnt = 0;
        	for(String key: keySet){
        		cnt += Long.parseLong(jedis.get(key));
        	}
        	
        	return cnt;
		} finally {
			jedisConnection.close();
		}
    }
    
    public void set(String key, String value) {
        Cache cache = cacheManager.getCache(DEFAULT_CACHE);
        cache.put(key, value);
    }
    
    public String get(String key) {
        Cache cache = cacheManager.getCache(DEFAULT_CACHE);
        if(cache.get(key) == null){
        	return "";
        }
        
        return cache.get(key).get().toString();
    }
    
    public void set(String key, String value, String cacheName) {
        Cache cache = cacheManager.getCache(cacheName);
        cache.put(key, value);
    }
    
    public String get(String key, String cacheName) {
        Cache cache = cacheManager.getCache(cacheName);
        if(cache.get(key) == null){
        	return "";
        }
        
        return cache.get(key).get().toString();
    }

    /**
     * 判断订单是否存在
     */
    public boolean existOrderFlag(String cacheName,String key) {
        Cache cache = cacheManager.getCache(cacheName);
        return cache.get(key) != null ? true : false;
    }

    /**
     * 添加订单
     */
    public void setPayFlag(String cacheName,String key) {
        Cache cache = cacheManager.getCache(cacheName);
        cache.put(key, "1");
    }
    
    public void evict(String cacheName,String key) {
        Cache cache = cacheManager.getCache(cacheName);
        cache.evict(key);
    }
    
}
